package com.irdstudio.efp.nls.api.rest.queue;

import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;
import com.irdstudio.efp.nls.service.facade.queue.NlsProcessRuleService;
import com.irdstudio.efp.nls.service.vo.queue.NlsProcessRuleVO;

/**
 * Controller： 网贷规则过程
 * 
 * @author huangwb
 * @since 2018年12月27日 上午9:04:14
 * @version 1.0
 */
@RestController
@RequestMapping("/api")
public class NlsProcessRuleController extends AbstractController {

	@Autowired
	@Qualifier("nlsProcessRuleService")
	private NlsProcessRuleService nlsProcessRuleService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	@PostMapping(value = "/nls/process/rules")
	public @ResponseBody ResponseData<List<NlsProcessRuleVO>> queryNlsProcessRuleAll(@RequestBody NlsProcessRuleVO vo) {
		// 根据用户角色获取用户权限方案
		List<NlsProcessRuleVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);

		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = nlsProcessRuleService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = nlsProcessRuleService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = nlsProcessRuleService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = nlsProcessRuleService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键查询详情
	 * 
	 * @return
	 */
	@GetMapping(value = "/nls/process/rule")
	public @ResponseBody ResponseData<NlsProcessRuleVO> queryByPk() {
		NlsProcessRuleVO inVo = new NlsProcessRuleVO();
		NlsProcessRuleVO outputVo = nlsProcessRuleService.queryByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 * 
	 * @param nlsProcessRule
	 * @return
	 */
	@DeleteMapping(value = "/nls/process/rule")
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody NlsProcessRuleVO inNlsProcessRuleVo) {
		int outputVo = nlsProcessRuleService.deleteByPk(inNlsProcessRuleVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * 
	 * @param inNlsProcessRuleVo
	 * @return
	 */
	@PutMapping(value = "/nls/process/rule")
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody NlsProcessRuleVO inNlsProcessRuleVo) {
		int outputVo = nlsProcessRuleService.updateByPk(inNlsProcessRuleVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * 
	 * @param inNlsProcessRuleVo
	 * @return
	 */
	@PostMapping(value = "/nls/process/rule")
	public @ResponseBody ResponseData<Integer> insertNlsProcessRule(@RequestBody NlsProcessRuleVO inNlsProcessRuleVo) {
		int outputVo = nlsProcessRuleService.insertNlsProcessRule(inNlsProcessRuleVo);
		return getResponseData(outputVo);

	}
}
